package com.dorm.cj.extension.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
@Aspect
@Component
public class AuditAspect {

    @Before("@annotation(com.dorm.cj.extension.aop.MyAudit)")
    public void beforeAdvice(JoinPoint joinPoint) {
        System.out.println("beforeAdvice=======" + joinPoint.toLongString());
        System.out.println("beforeAdvice=======" + joinPoint.getTarget());
    }

    @After("@annotation(com.dorm.cj.extension.aop.MyAudit)")
    public void afterAdvice(JoinPoint joinPoint) {
        System.out.println("afterAdvice=======" + joinPoint.toLongString());
        System.out.println("afterAdvice=======" + joinPoint.getTarget());
    }

    @Around("@annotation(com.dorm.cj.extension.aop.MyAudit)")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("aroundAdvice=======" + joinPoint.toLongString());
        System.out.println("aroundAdvice=======" + joinPoint.getTarget());
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        MyAudit annotation = method.getAnnotation(MyAudit.class);
        String operation = annotation.operation();
        System.out.println("aroundAdvice=======" + operation);
        System.out.println("aroundAdvice========方法调用前");
        Object proceed = joinPoint.proceed();

        System.out.println("aroundAdvice========方法调用后");
        return proceed;
    }
}
